'use strict';

const Controller = require('egg').Controller;
const fs = require("fs")
const path = require("path")
class BReleaseController extends Controller {
  async index() {
    const {
      ctx
    } = this;
    let f1 = ctx.request.files
    let ziduan = ctx.request.body
    // console.log(44,ziduan,1);
    // console.log(f1,22);
    ctx.body = '成功';
    // console.log(ziduan.name1);

    if (ziduan.name1) {
      // 获取缓存中的邮箱
      let email = this.ctx.session.email
      let user = `SELECT * FROM user WHERE email="${email}"`
      let userInfo = await this.app.mysql.query(user)
      // console.log(userInfo);

      // 转移图片路径
      let oldpath = f1[0].filepath
      let newpath = __dirname + "/../public/cover/" + path.basename(oldpath)
      fs.copyFile(oldpath, newpath, (err) => {})
      ziduan[f1[0].field] = "/public/cover/" + path.basename(oldpath)

      let text = await this.ctx.service.escape.escape(ziduan.introduction)


      // 将数据存进数据库
      let sql1 = `INSERT INTO book (name1, introduction, classify, cover,userName,id) VALUES ("${ziduan.name1}", "${text}", "${ziduan.classify}","${ziduan[f1[0].field]}", "${userInfo[0].username}", "${userInfo[0].id}")`
      let result1 = await this.app.mysql.query(sql1)
      console.log(67);
      if(result1){
        ctx.body = {info:{cood:200}};
      }else{
        ctx.body = {info:{cood:400}}
      }
      
    } else {

    }




  }

}

module.exports = BReleaseController;